package top.oauth2.gateway.config;


import cn.hutool.core.util.ArrayUtil;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
import org.springframework.security.oauth2.server.resource.authentication.ReactiveJwtAuthenticationConverterAdapter;
import org.springframework.security.web.server.SecurityWebFilterChain;
import reactor.core.publisher.Mono;
import top.oauth2.common.constant.AuthConstant;
import top.oauth2.gateway.component.AuthorizationManager;
import top.oauth2.gateway.filter.IgnoreUrlsRemoverJwtFilter;
import top.oauth2.gateway.component.RestAuthenticationEntryPoint;
import top.oauth2.gateway.component.RestfulAccessDeniedHandler;

/**
 * 资源服务器配置
 * @author xphu
 * @version 1.0
 * @date 2021/8/30 14:09
 */
@AllArgsConstructor
@Configuration
@EnableWebFluxSecurity
public class ResourceServerConfig {
    /**
     * 自定义鉴权操作
     */
    private final AuthorizationManager authorizationManager;
    /**
     * 白名单
     */
    private final IgnoreUrlsConfig ignoreUrlsConfig;
    /**
     * 自定义返回结果：没有权限访问时
     */
    private final RestfulAccessDeniedHandler restfulAccessDeniedHandler;

    /**
     * 认证入口点 处理：自定义返回结果：没有登录或token过期时
     */
    private final RestAuthenticationEntryPoint restAuthenticationEntryPoint;

    /**
     * 白名单处理
     */
    private final IgnoreUrlsRemoverJwtFilter ignoreUrlsRemoverJwtFilter;


    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http){
        http.oauth2ResourceServer().jwt()
                .jwtAuthenticationConverter(jwtAuthenticationConverter());
        // 自定义处理 没有登录或token过期
        http.oauth2ResourceServer().authenticationEntryPoint(restAuthenticationEntryPoint);
        http.authorizeExchange()
                //白名单配置
                .pathMatchers(ArrayUtil.toArray(ignoreUrlsConfig.getUrls(),String.class)).permitAll()
                //鉴权管理器配置
                .anyExchange().access(authorizationManager)
                .and().exceptionHandling()
                //处理未授权
                .accessDeniedHandler(restfulAccessDeniedHandler)
                //处理未认证
                .authenticationEntryPoint(restAuthenticationEntryPoint)
                // 关闭csrf 跨站伪造安全
                .and().csrf().disable();
        return http.build();
    }

    @Bean
    public Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> jwtAuthenticationConverter() {
        JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
        // 设置权限前缀 设置权限声明名称
        jwtGrantedAuthoritiesConverter.setAuthorityPrefix(AuthConstant.AUTHORITY_PREFIX);
        // 设置权限名称
        jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName(AuthConstant.AUTHORITY_CLAIM_NAME);
        // 设置 Jwt 授予权限转换器
        JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
        jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);
        return new ReactiveJwtAuthenticationConverterAdapter(jwtAuthenticationConverter);
    }
}
